package by.dkusch.aircompany.webapp.page.staff.panel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TreeSet;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.CollectionModel;
import org.apache.wicket.model.util.ListModel;

import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Crew_;
import by.dkusch.aircompany.model.tables.Person;
import by.dkusch.aircompany.services.CrewService;
import by.dkusch.aircompany.services.PersonService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.validator.UniqueFieldValidator;

public class CrewEditPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private CrewService crewService;
	@Inject
	private PersonService personService;

	private Locale selectedLocale;

	public CrewEditPanel(String id, final Crew crew, final PageCreator pageCreator, ActionType action) {
		super(id);
		selectedLocale = Session.get().getLocale();

		Form<Crew> form = new Form<Crew>("form", new CompoundPropertyModel<Crew>(crew));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>()).setLabel(new ResourceModel("crew.list.name"));
		form.add(tfName);

		final DateTextField tfStartDate = new DateTextField("startDate", new StyleDateConverter("S-", false)) {

			private static final long serialVersionUID = 1L;

			@Override
			public Locale getLocale() {
				return selectedLocale;
			}
		};
		tfStartDate.add(new PropertyValidator<Date>()).setLabel(new ResourceModel("crew.list.date.start"));
		form.add(tfStartDate);

		DatePicker datePickerStart = new DatePicker() {
			private static final long serialVersionUID = 1L;

			@Override
			protected String getAdditionalJavaScript() {
				return "${calendar}.cfg.setProperty(\"navigator\",true,false); ${calendar}.render();";
			}
		};
		datePickerStart.setShowOnFieldClick(false);
		datePickerStart.setAutoHide(true);
		tfStartDate.add(datePickerStart);

		final DateTextField tfEndDate = new DateTextField("endDate", new StyleDateConverter("S-", false)) {

			private static final long serialVersionUID = 1L;

			@Override
			public Locale getLocale() {
				return selectedLocale;
			}
		};
		tfEndDate.add(new PropertyValidator<Date>()).setLabel(new ResourceModel("crew.list.date.end"));
		form.add(tfEndDate);

		DatePicker datePickerEnd = new DatePicker() {
			private static final long serialVersionUID = 1L;

			@Override
			protected String getAdditionalJavaScript() {
				return "${calendar}.cfg.setProperty(\"navigator\",true,false); ${calendar}.render();";
			}
		};
		datePickerEnd.setShowOnFieldClick(false);
		datePickerEnd.setAutoHide(true);
		tfEndDate.add(datePickerEnd);

		final TextArea<String> tfDescription = new TextArea<String>("description");
		tfDescription.add(new PropertyValidator<String>()).setLabel(new ResourceModel("crew.list.description"));
		form.add(tfDescription);

		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>()).setLabel(new ResourceModel("crew.list.arcfl"));
		form.add(chBArc);

		form.add(new Label("correctDt"));

		List<Person> persons = personService.getAll();
		crew.setPersonSet(crewService.getPersonSet(crew.getId()));

		IChoiceRenderer<Person> renderer = new ChoiceRenderer<Person>("uFIO", "id");
		final List<Person> selected = new ArrayList<Person>(crew.getPersonSet());
		final Palette<Person> palette = new Palette<Person>("palette", new ListModel<Person>(selected), new CollectionModel<Person>(persons), renderer, 10, false);

		form.add(palette);

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				super.onSubmit();
				crew.setPersonSet(new TreeSet<Person>(selected));
				crewService.saveOrUpdate(crew);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

		};
		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		final TextField<String> tfCode = new TextField<String>("code");
		tfCode.add(new PropertyValidator<String>()).setLabel(new ResourceModel("crew.list.code"));
		tfCode.add(new UniqueFieldValidator<Long, Crew>(Crew_.code, crewService, crew)).setLabel(new ResourceModel("crew.list.code"));
		form.add(tfCode);

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});

		add(form);
		form.setEnabled(action.equals(ActionType.EDIT));

	}

}
